package com.icode.web.controller.rent;

import com.icode.core.dto.house.RentDTO;
import com.icode.core.dto.manager.ReserveDTO;
import com.icode.core.model.house.MyHouseStatus;
import com.icode.core.model.manage.Reserve;
import com.icode.core.model.manage.ReserveTime;
import com.icode.core.model.user.User;
import com.icode.core.shared.RentAlipayHandle;
import com.icode.core.shared.SecurityUtil;
import com.icode.service.house.MyHouseService;
import com.icode.service.house.ReserveService;
import com.icode.service.management.RentService;
import com.icode.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sunk
 * Date: 15-11-6
 * Time: 下午6:33
 * To change this template use File | Settings | File Templates.
 */
@Controller("RentController")
@RequestMapping("/rent")
public class RentController {
    @Autowired
    private RentService rentService;
    @Autowired
    private ReserveService reserveService;

    private RentDTO rentDTOReceive;


    @RequestMapping("init")
    @ResponseBody
    public Object rentForm(@RequestBody RentDTO rentDTO) {
        Map<String, Boolean> model = new HashMap();
        boolean isSuccess = rentService.handleRent(rentDTO);
        model.put("isSuccess", isSuccess);
        return model;
    }

    @RequestMapping("renttime/{myhouseguid}")
    @ResponseBody
    public List<ReserveDTO> renttime(@PathVariable String myhouseguid){
        List<ReserveDTO> reserveDTOs = reserveService.findByMyHouseguid(myhouseguid);
        return reserveDTOs;
    }

    @RequestMapping("list")
    public String listForRent(Model model) {
//        List<RentDTO> rentDTOs = rentService.findMyHouseByStatus(MyHouseStatus.TORENT, type);
        List<RentDTO> rentDTOs = rentService.findMyHouseByStatusNoType(MyHouseStatus.TORENT);
        model.addAttribute("rentDTOs", rentDTOs);
        return "front/rent/list";
    }

    @RequestMapping(value = "search", method = RequestMethod.POST)
    public String search(String area, String week, String themes, String price, Model model) {
        List<RentDTO> rentDTOs = rentService.findMyHouseByCondition(area, week, themes, price);
        model.addAttribute("rentDTOs", rentDTOs);
        return "front/rent/listdata";
    }

    @RequestMapping("details/{rentGuid}")
    public String details(@PathVariable String rentGuid, Model model) {
        RentDTO rentDTO = rentService.findRentByGuid(rentGuid);
        model.addAttribute("rentDTO", rentDTO);
        return "front/rent/detail";
    }


    @RequestMapping("pay/{rentGuid}")
    public String pay(@PathVariable String rentGuid, Model model) {
        RentDTO rentDTO = rentService.findRentByGuid(rentGuid);
        model.addAttribute("rentDTO", rentDTO);
        return "front/rent/pay";
    }

    @RequestMapping(value = "payment", method = RequestMethod.POST)
    public void payFor(RentDTO rentDTO, PrintWriter out) {
        rentDTOReceive = rentDTO;
        String price = rentDTO.getPrice();
        String houseName = rentDTO.getHouseName();
        String guid = rentDTO.getGuid();
        RentAlipayHandle.alipay(price, houseName, guid, out);
    }

    @RequestMapping("notify_url")
    public String notify_url(HttpServletRequest request, PrintWriter out) {
        Map<Object, Object> result = RentAlipayHandle.notify_url(request, out,rentService,rentDTOReceive);
        boolean isSuccess = (boolean) result.get("isSuccess");
        if (isSuccess) {
            String rentGuid = (String) result.get("rentGuid");
        }
        return "redirect:/my_house/show";
    }

    @RequestMapping("return_url")
    public String return_url(HttpServletRequest request, PrintWriter out) {
        User user = SecurityUtil.currentUser();
        String url = "";
        Map<Object, Object> result = RentAlipayHandle.return_url(request, out,rentService,rentDTOReceive);
        boolean isSuccess = (boolean) result.get("isSuccess");
        if (isSuccess) {
            String rentGuid = (String) result.get("rentGuid");
            String myHouseGuid = rentService.updateRent(user, rentGuid);
            url = "redirect:/my_house/show";
        }
        return url;
    }
}
